package net.wanho.controller;

import lombok.RequiredArgsConstructor;
import net.wanho.po.ProductType;
import net.wanho.service.ProductService;
import net.wanho.service.ProductTypeService;
import net.wanho.vo.AjaxResult;
import org.springframework.web.bind.annotation.*;

import java.util.List;

@RestController
@RequiredArgsConstructor
@RequestMapping("/productCategory")
public class ProductTypeController {

    private final ProductService productService;

    private final ProductTypeService productTypeService;

    @GetMapping("/product-category-tree")
    public AjaxResult productCategoryTree() {
        List<ProductType> productType = productTypeService.selectAll();
        return AjaxResult.success("success",  productType);
    }

    @GetMapping("/verify-type-code")
    public AjaxResult verifyTypeCode(@RequestParam String typeCode) {
        if(productTypeService.selectByCode(typeCode)){
            return AjaxResult.error("该分类编码已存在");
        };
        return AjaxResult.success("该分类编码可用");
    }

    @PostMapping("/type-add")
    public AjaxResult typeAdd(@RequestBody ProductType productType) {
        productTypeService.save(productType);
        return AjaxResult.success("添加成功");
    }

    @GetMapping("/{id}")
    public AjaxResult getProductTypeById(@PathVariable Integer id) {
        ProductType productType = productTypeService.getById(id);
        return AjaxResult.success("success", productType);
    }

    @PutMapping("/type-update")
    public AjaxResult typeUpdate(@RequestBody ProductType productType) {
        productTypeService.updateById(productType);
        return AjaxResult.success("修改成功");
    }

    @DeleteMapping("/type-delete/{id}")
    public AjaxResult typeDelete(@PathVariable Integer id) {
        productTypeService.deleteById(id);
        return AjaxResult.success("分类删除成功");
    }
}
